<?php

namespace app\admin\model\shop\goods;

use ba\Exception;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * Service
 */
class Service extends Model
{
    use SoftDelete;

    protected $defaultSoftDelete = 0;

    // 表名
    protected $name = 'shop_goods_service';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    /**
     * @throws Exception
     */
    public static function onBeforeDelete(Model $model): Model
    {
        $count = (new Goods)->whereFindInSet('service_id', $model->id)->count();
        if ($count > 0) {
            throw new Exception('该模板已被商品绑定使用，请先解绑后再删除');
        }

        return $model;
    }
}